package com.yida.system.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson2.JSONObject;
import com.yida.common.core.domain.R;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.core.utils.sign.RsaUtils;
import com.yida.common.core.web.controller.BaseController;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.core.web.page.TableDataInfo;
import com.yida.common.log.annotation.Log;
import com.yida.common.log.enums.BusinessType;
import com.yida.common.security.annotation.InnerAuth;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.common.security.utils.SecurityUtils;
import com.yida.system.api.domain.SysUser;
import com.yida.system.api.domain.SysUserUkey;
import com.yida.system.service.ISysAdminPasswordService;
import com.yida.system.service.ISysUserUkeyService;

/**
 * @author hmh
 * @date 2023/03/28
 */
@RestController
@RequestMapping("/ukey")
public class SysUKeyUserController extends BaseController {

    // private ConcurrentHashMap map = new ConcurrentHashMap();

    @Autowired
    private ISysUserUkeyService iSysUserUkeyService;

    @Autowired
    private ISysAdminPasswordService iSysAdminPasswordService;

    @InnerAuth
    @PostMapping("/waitSignMsg")
    public R<SysUserUkey> waitSignMsg(@RequestBody SysUserUkey sSysUKeyUser) {
        SysUserUkey sysUserUkey = iSysUserUkeyService.getWaitSignMsgByUserName(sSysUKeyUser.getUserName(), sSysUKeyUser.getUkeyId());
        if (StringUtils.isNull(sysUserUkey)) {
            return R.fail("[C]未获取到对应Ukey信息，请确认当前设备是否本人持有！");
        }
        return R.ok(sysUserUkey);
    }

    @InnerAuth
    @PostMapping("/validate")
    public R<Boolean> ukeyValidate(@RequestBody SysUserUkey sSysUKeyUser) {
        if (iSysUserUkeyService.checkSign(sSysUKeyUser)) {
            return R.ok();
        }
        return R.fail("[C]UKey校验失败，请确认当前设备是否本人持有！");
    }

    /**
     * 查询地区管理列表
     */
    @RequiresPermissions("system:ukey:list")
    @Log(title = "用户UKey管理", businessType = BusinessType.GRANT)
    @GetMapping("/list")
    public TableDataInfo list(SysUserUkey sSysUKeyUser) {
        startPage();
        List<SysUserUkey> list = iSysUserUkeyService.selectSysUserUkeyList(sSysUKeyUser);
        return getDataTable(list);
    }

    @RequiresPermissions("system:ukey:list")
    @Log(title = "用户UKey管理", businessType = BusinessType.GRANT)
    @GetMapping("/pin")
    public AjaxResult getPin() {
        return AjaxResult.success(iSysUserUkeyService.getPin());
    }

    /**
     * 校验密码返回详情
     */
    @RequiresPermissions("system:ukey:edit")
    @Log(title = "用户UKey管理", businessType = BusinessType.GRANT)
    @PostMapping("/info")
    public AjaxResult getInfo(@RequestBody SysUserUkey sysUKeyUser) throws Exception {
        // 获取登录用户ID
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        if (StringUtils.isNull(sysUKeyUser.getPassword())) {
            return AjaxResult.error("请输入登录密码！");
        }
        String passwordInfo = RsaUtils.decryptByPrivateKey(sysUKeyUser.getPassword());
        // 1校验当前操作用户密码是否一致
        iSysAdminPasswordService.validate(sysUser, passwordInfo);
        // 2获取查询用户数据
        SysUserUkey sSysUKeyUser = iSysUserUkeyService.selectSysUserKeyInfo(sysUKeyUser.getUserId());
        // 过滤输出
        return AjaxResult.success(JSONObject.from(sSysUKeyUser));
    }

    // 新增绑定
    @RequiresPermissions("system:ukey:add")
    @Log(title = "用户UKey管理", businessType = BusinessType.INSERT_UPDATE)
    @PostMapping("/bindUkeyByUserId")
    public AjaxResult bindUkeyByUserId(@RequestBody SysUserUkey sysUKeyUser) {
        if (iSysUserUkeyService.bindUkeyByUserId(sysUKeyUser)) {
            return AjaxResult.success("绑定成功！");
        }
        return AjaxResult.error("绑定成功");
    }

    // 解绑
    @RequiresPermissions("system:ukey:remove")
    @Log(title = "用户UKey管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/unbindingUkey/{userId}")
    public AjaxResult delUkeyInfo(@PathVariable Long userId) {
        if (iSysUserUkeyService.delUkeyInfo(userId) > 0) {
            return AjaxResult.success("解绑成功！");
        }
        return AjaxResult.error("解绑失败！");
    }

    // 切换状态
    @RequiresPermissions("system:ukey:edit")
    @Log(title = "用户UKey管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody SysUserUkey sysUKeyUser) {
        if (iSysUserUkeyService.changeUkeyStatus(sysUKeyUser) > 0) {
            return AjaxResult.success("切换成功！");
        }
        return AjaxResult.error("切换失败！");
    }

}
